package com.googlecode.agscrum.model.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Transient;

import com.googlecode.agscrum.model.exceptions.GScrumDomainException;
import com.googlecode.agscrum.model.exceptions.MessageFactory;
import com.googlecode.agscrum.model.exceptions.GScrumDomainException.TypeMessage;

@Entity
@NamedQueries(value = {
		@NamedQuery(name = "Team.searchQuery", query = "select new Team(t.id, t.name) from Team t"),
		@NamedQuery(name = "Team.duplicateValidate", query = "select count(*) as total from Team t where t.name=:name") })
@SequenceGenerator(name = "team_sequence", sequenceName = "team_sequence")
public class Team implements Serializable {
	@Id
	@GeneratedValue(generator = "team_sequence", strategy = GenerationType.SEQUENCE)
	@Column(name = "id_team")
	private Long id;
	@Column(nullable = false, length = 25)
	private String name;
	@ManyToMany
	@JoinTable(name = "TEAM_USER", joinColumns = @JoinColumn(name = "id_team", referencedColumnName = "id_team"), inverseJoinColumns = @JoinColumn(name = "id_user", referencedColumnName = "id_user"))
	private List<User> users;

	public Team() {
	}

	public Team(Long id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public void addUser(User user) {
		if (users == null) {
			users = new ArrayList<User>();
		}
		users.add(user);
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	@Transient
	private MessageFactory messageFactory;

	public void setMessageFactory(MessageFactory messageFactory) {
		this.messageFactory = messageFactory;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Team other = (Team) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	public List<User> getUsers() {
		if (users != null)
			return Collections.unmodifiableList(users);
		else
			return users;
	}

	public void changeUsers(List<User> selectedUsers)
			throws GScrumDomainException {
		if (selectedUsers == null || selectedUsers.size() == 0) {
			throw new GScrumDomainException(messageFactory.createMessage(
					"team.users.required", null, TypeMessage.ERROR));
		} else {
			if (users == null) {
				users = new ArrayList<User>();
			} else {
				users.clear();
			}
			users.addAll(selectedUsers);
		}

	}

}
